//index.js
//获取应用实例
import * as API from "../../utils/config.js"
const app = getApp()
Page({
  data: {
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular: true,
    indicatoractivecolor:'#f5f5f5',
    list: [],
    recommendName:"耽美漫画",
    recommendData: []
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },
  onLoad:function(e){
    this._initData()
    this._contentData()
    this._getRecommend()
  },
  //轮播数据
  _initData(){
    var lunboData = wx.getStorageSync('lunbo')
    console.log("轮播!!!!",lunboData)
    // console.log(Boolean(lunboData.length))
    if (lunboData.length){//判断是否有本地缓存
      this.setData({
        imgUrls: lunboData
      })
    }else{//无本地缓存则请求
      wx.request({
        url: API.swiperApi,
        success: (res) => {
          this.setData({
            imgUrls: res.data.data
          })
          app.sStorageSync('lunbo', this.data.imgUrls)
        }
      })
    }  
  },
  //"推荐"数据
  _getRecommend(){
    var tjData = wx.getStorageSync('classifyData3')
    console.log(tjData)
    console.log(this.data.recommendName)
    if (tjData) {//判断是否有本地缓存
      this.setData({
        recommendData: tjData
      })
    } else {//无本地缓存则请求
      wx.request({
        url: API.book,
        data: {
          "key": API.key,
          "type": this.data.recommendName
        },
        success: (res) => {
          console.log(res)
          this.setData({
            recommendData: res.data.result.bookList,
          })
          app.sStorageSync('classifyData3',this.data.recommendData,'index_recommend')
          // try {
          //   wx.setStorageSync('classifyData3', this.data.recommendData)
          // } catch (e) {
          //   console.log("_getRecommend", e)
          // }
        }
      })
    }
  },
  //"大"数据
  _contentData(){
    var listshujv = wx.getStorageSync('listData')
    if (listshujv) {//判断是否有本地缓存
      this.setData({
        list: listshujv
      })
    } else {//无本地缓存则请求
      wx.request({
        url: API.book,
        data: {
          "key": API.key
        },
        success: (res) => {
          this.setData({
            list: res.data.result.bookList,
          })
          try {
            wx.setStorageSync('listData', this.data.list)
          } catch (e) {
            console.log("list_err", e)
          }
        }
      })
    }
  },
  tochapter(e){
    app.tochapter(e)
  }
})
